package com.springcloud.sc.ucpv5.service.impl;

import com.alibaba.fastjson.JSON;
import com.springcloud.sc.ucpv5.client.commondata.MyGroup;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.commondata.MyUserInfoOtherData;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;
import com.springcloud.sc.ucpv5.service.GroupDetailService;
import com.springcloud.sc.ucpv5.service.UcpService;
import com.springcloud.sc.ucpv5.tree.GroupNode;
import com.springcloud.sc.ucpv5.tree.UserNode;
import com.springcloud.sc.ucpv5.tree.VoiceAuthorityNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GroupDetailServiceImpl implements GroupDetailService {

    @Autowired
    private UcpService ucpService;

    public GroupNode getGroupNodeByGroup(MyGroup myGroup) {

        GroupNode groupNode = new GroupNode();
        if(myGroup!=null){
            //表示是应急组
            if(myGroup.getIdlData().getGroupType() == 2 ){
                groupNode.setName( myGroup.getIdlData().getGroupName() );
                groupNode.setNumber( myGroup.getIdlData().getGroupTelno() );
                List<MyUser> myUserList = myGroup.getListModel().getUserList();

                // wsy 20190604 处理组的编号下发
                if(myGroup.getIdlData().getKeep1() != null && !myGroup.getIdlData().getKeep1().equals("")){
                    groupNode.setSign(myGroup.getIdlData().getKeep1());
                }

                if(myUserList!=null){
                    List<UserNode> userList = new ArrayList<>();
                    myUserList.forEach(user ->{
                        UserNode userNode = new UserNode();
                        int userType = user.getIdlData().getUserType();
                        int userTypeData = userType & 0x000000FF;
                        int tmp = (userType >> 10) & 0x0000000F;
                        userType = userTypeData * 10 + tmp;
                        if(userType == 1 || userType == 2 ){
                            userNode.setUserType( "1" );
                        }else{
                            userNode.setUserType( "0" );
                        }
                        userNode.setName( user.getIdlData().getUserName() );
                        userNode.setNumber( user.getIdlData().getUserTelno() );
                        userNode.setPassword( user.getIdlData().getPassword() );

                        try{
                            try {
                                // 从缓存额外信息的iconname字段中解析出来该用户可发起语音发布权限的组列表
                                MyUserInfoOtherData otherInfo = user.getUserInfoData().otherInfo;
                                if(otherInfo.voiceAuthorityGroupList != null && otherInfo.voiceAuthorityGroupList.size() > 0){
                                    // 如果组列表不为空, 则具有语音发布权限
                                    userNode.setVoiceAuthority("1");
                                    List<VoiceAuthorityNode> voiceAuthorityNodeList = new ArrayList<>();
                                    for(String groupNumber : otherInfo.voiceAuthorityGroupList){
                                        MyGroup tempGroup = BaseDataManager.getInstance().groupGet( groupNumber );
                                        if(tempGroup.getIdlData().getGroupType() == 2 ){
                                            VoiceAuthorityNode voiceAuthorityNode = new VoiceAuthorityNode();
                                            voiceAuthorityNode.setNumber( tempGroup.getIdlData().getGroupTelno() );
                                            voiceAuthorityNodeList.add( voiceAuthorityNode );
                                        }
                                    }
                                    userNode.setVoiceAuthorityGroupList( voiceAuthorityNodeList );
                                }
                                else{
                                    // 可发布的组为空, 则不具有发布权限
                                    userNode.setVoiceAuthority("0");
                                }
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        userList.add( userNode );
                    });
                    groupNode.setUserList( userList );
                }

            }
            List<MyGroup>  myGroupList = myGroup.getListModel().getGroupList();
            //表示还有子组
            if(myGroupList!=null && !myGroupList.isEmpty()){
                myGroupList.forEach(childMyGroup->{
                    setBranch( childMyGroup );
                });

            }


        }

        return groupNode;
    }

    public void setBranch(MyGroup myGroup ){
        try{
            ucpService.startBranchs( true , myGroup );
            Thread.sleep(20);
        }catch (InterruptedException ie){
            ie.printStackTrace();
        }
    }
}
